from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy


from config import CONFIG

# 创建SQLAlchemy对象
db = SQLAlchemy()


def create_app(config_name):
    from app.views.chg import chg_blu
    from app.views.index import index_blu
    from app.views.login import login_blu
    from app.views.admin import admin_blu
    from app.views.news import news_blu
    # 创建flask对象
    app = Flask(__name__)
    # 对flask对象进行配置
    app.config.from_object(CONFIG.get(config_name))
    # 注册蓝图
    app.register_blueprint(index_blu)
    app.register_blueprint(admin_blu, url_profix="/admin")
    app.register_blueprint(login_blu)
    app.register_blueprint(news_blu)
    app.register_blueprint(chg_blu, url_profix="/chg")
    # 对db进行配置
    db.init_app(app)
    @app.errorhandler(404)
    def page_404(e):
        return render_template("index/index_404.html"), 404
    return app
